using System;
using System.IO;
using GameFramework;
using UnityEditor;
using UnityEngine;

namespace StarForce.Editor
{
    public static class BuildHotfixEditor
    {
        private const string HotfixProjectPath = "./Library/ScriptAssemblies";
        private const string HotfixCodePath = "Assets/GameMain/HotfixDLL";

        private static readonly string HotfixDLL =
            Utility.Text.Format("{0}.Hotfix.dll", Constant.AssemblyInfo.Namespace);

        private static readonly string HotfixPDB =
            Utility.Text.Format("{0}.Hotfix.pdb", Constant.AssemblyInfo.Namespace);

        [MenuItem("ILRuntime/Build Hotfix DLL")]
        public static void GeneratorHotfixDLL()
        {
            DirectoryInfo hotfixDLLPath = new DirectoryInfo(HotfixProjectPath);
            if (!Directory.Exists(HotfixCodePath))
            {
                Directory.CreateDirectory(HotfixCodePath);
            }

            try
            {
                File.Copy(Path.Combine(hotfixDLLPath.FullName, HotfixDLL),
                    Path.Combine(HotfixCodePath, Utility.Text.Format("{0}.bytes", HotfixDLL)), true);
                File.Copy(Path.Combine(hotfixDLLPath.FullName, HotfixPDB),
                    Path.Combine(HotfixCodePath, Utility.Text.Format("{0}.bytes", HotfixPDB)), true);
            }
            catch (Exception e)
            {
                Debug.LogError(e.StackTrace);
                Directory.Delete(HotfixCodePath, true);
            }

            Debug.Log("构建DLL成功");
            AssetDatabase.Refresh();
        }
    }
    
}